package com.bawei.fuxi.ui.login;



import com.bawei.fuxi.base.mvp.BasePresenter;
import com.bawei.fuxi.entity.LoginEntity;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;

public class LoginPresenter extends BasePresenter<LoginContract.ILoginView> implements LoginContract.ILoginPresenter {
    private LoginContract.ILoginModel mILoginModel;
    public LoginPresenter() {
        mILoginModel = new LoginModel();
    }

    @Override
    public void login(RequestBody requestBody) {
        mILoginModel.login(requestBody)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<LoginEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mCompositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(LoginEntity loginEntity) {
                        if(isAttach()){
                            getView().showLogin(loginEntity);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        if(isAttach()){
                            getView().onError();
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
